package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.AmortiseSchema;
import org.hgvc.webapp.ets.bo.Membership;
import org.hgvc.webapp.ets.bo.MembershipCategory;
import org.hgvc.webapp.ets.bo.PointCategory;
import org.hgvc.webapp.ets.bo.PointMembershipMap;
import org.hgvc.webapp.ets.constant.MembershipConstant;
import org.hgvc.webapp.ets.spring.service.MembershipService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class AdminMembershipBean extends AbstractBean {

	public static final String BEAN_NAME="adminMembershipBean";
	public static final String NAV_LIST="adminMemList";
	public static final String NAV_CREATE="adminMemCreate";
	public static final String NAV_EDIT="adminMemEdit";

	private MembershipService service;
	private Membership membership;
	private List<Membership> membershipList;
	private List<SelectItem> categoryList;
	private List<SelectItem> amortiseList;
	private List<SelectItem> typeList;
	private List<PointMembershipMap> mapList;
	private List<PointCategory> pcategoryList;
	private String schId;
	private String catId;

	public Membership getMembership() {
		return membership;
	}
	public void setMembership(Membership membership) {
		this.membership = membership;
	}
	public void setService(MembershipService service) {
		this.service = service;
	}
	public String getSchId() {
		return schId;
	}
	public void setSchId(String schId) {
		this.schId = schId;
	}
	public String getCatId() {
		return catId;
	}
	public void setCatId(String catId) {
		this.catId = catId;
	}
	public List<PointMembershipMap> getMapList() {
		return mapList;
	}
	public void setMapList(List<PointMembershipMap> mapList) {
		this.mapList = mapList;
	}
	/*------------------------------------------------------------*/
	public String navListAction(){
		return NAV_LIST;
	}

	public List<Membership> getMembershipList() {
		try{
			membershipList = service.getMembershipList();
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return membershipList;
	}

	public String navCreateAction(){
		membership = new Membership();
		mapList = new ArrayList<PointMembershipMap>();
		for(PointCategory p:getPcategoryList()){
			PointMembershipMap map = new PointMembershipMap();
			map.setPointCategory(p);
			mapList.add(map);
		}
		return NAV_CREATE;
	}

	public String createAction(){
		try{
			membership.setAmortiseSchema(new AmortiseSchema(schId));
			membership.setMembershipCategory(new MembershipCategory(catId));
			service.createMembership(membership, getThisUser(),mapList);
			return NAV_LIST;
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_CREATE;
	}

	public void editActionListener(ActionEvent ae){
		try{
			UIParameter param = (UIParameter)ae.getComponent().findComponent("memId");
			String id = (String)param.getValue();
			for(Membership mem:membershipList){
				if(mem.getMembershipId().equalsIgnoreCase(id)){
					membership = mem;
					schId = membership.getAmortiseSchema().getAmortiseSchId();
					catId = membership.getMembershipCategory().getMembershipCatId();
					mapList = service.getPointMembershipMapList(membership);
					break;
				}
			}
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
	}

	public String navEditAction(){
		return NAV_EDIT;
	}

	public String editAction(){
		try{
			membership.setAmortiseSchema(new AmortiseSchema(schId));
			membership.setMembershipCategory(new MembershipCategory(catId));
			service.editMembership(membership,getThisUser(),mapList);
			return NAV_LIST;
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_EDIT;
	}

	public String deleteAction(){
		try{
			service.deleteMembership(membershipList,getThisUser());
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return NAV_LIST;
	}
	public List<SelectItem> getCategoryList() {
		try{
			categoryList = new ArrayList<SelectItem>();
			List<MembershipCategory> list = service.getMembershipCategoryList();

			for(MembershipCategory cat:list){
				SelectItem item = new SelectItem(cat.getMembershipCatId(),cat.getCode()+" "+cat.getName());
				categoryList.add(item);
			}
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}

		return categoryList;
	}
	public List<SelectItem> getAmortiseList() {
		try{
			amortiseList = new ArrayList<SelectItem>();
			List<AmortiseSchema> list = service.getAmortiseSchemaList();

			for(AmortiseSchema s:list){
				SelectItem item = new SelectItem(s.getAmortiseSchId(),s.getCode()+" "+s.getName());
				amortiseList.add(item);
			}
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return amortiseList;
	}
	public List<SelectItem> getTypeList() {
		typeList = new ArrayList<SelectItem>();

		SelectItem item = new SelectItem(MembershipConstant.MEMBERSHIP_TYPE_FULL,
				SpringUtils.getMessage(MembershipConstant.MEMBERSHIP_TYPE_FULL, new Object[]{}));
		SelectItem item2 = new SelectItem(MembershipConstant.MEMBERSHIP_TYPE_EVEN_OR_ODD,
				SpringUtils.getMessage(MembershipConstant.MEMBERSHIP_TYPE_EVEN_OR_ODD, new Object[]{}));
		/*SelectItem item2 = new SelectItem(MembershipConstant.MEMBERSHIP_TYPE_EVEN,
				SpringUtils.getMessage(MembershipConstant.MEMBERSHIP_TYPE_EVEN, new Object[]{}));
		SelectItem item3 = new SelectItem(MembershipConstant.MEMBERSHIP_TYPE_ODD,
				SpringUtils.getMessage(MembershipConstant.MEMBERSHIP_TYPE_ODD, new Object[]{}));*/
		typeList.add(item);
		typeList.add(item2);
		//typeList.add(item3);
		return typeList;
	}
	public List<PointCategory> getPcategoryList() {
		try{
			pcategoryList = service.getPointCategoryList();
		}catch(BaseException be){
			initErrorBean(be);
			be.printStackTrace();
		}
		return pcategoryList;
	}

}
